﻿#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

#define sys_out qDebug()

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("OCR文字识别综合应用  作者：香菇滑稽  blog.csdn.net/wwplh5520370  wechat：784765727");

    ButtonGroup = new QButtonGroup(this);
    ButtonGroup->addButton(ui->radioButton_1,0);
    ButtonGroup->addButton(ui->radioButton_2,1);
    ButtonGroup->addButton(ui->radioButton_3,2);
    ButtonGroup->addButton(ui->radioButton_4,3);
    ButtonGroup->addButton(ui->radioButton_5,4);
    ButtonGroup->addButton(ui->radioButton_6,5);
    ButtonGroup->addButton(ui->radioButton_7,6);
    ButtonGroup->addButton(ui->radioButton_8,7);
    ButtonGroup->addButton(ui->radioButton_9,8);
    ui->radioButton_1->setChecked(true);

    swidget = new ScreenWidget;
    connect(swidget,&ScreenWidget::overShot,this,&MainWindow::drawPixmap);
    connect(swidget,&ScreenWidget::cancelShot,[this] {setWindowState(Qt::WindowNoState);});

    BaiduOCR = new BaiduOCROnline(this);
    ui->image_path->setReadOnly(true);

    connect(BaiduOCR,&BaiduOCROnline::StdOCRResult,this,&MainWindow::displayResult);
    connect(BaiduOCR,&BaiduOCROnline::PreciseOCRResult, this,&MainWindow::displayResult);
    connect(BaiduOCR,&BaiduOCROnline::idCardScanResult,this,&MainWindow::displayResult);
    connect(BaiduOCR,&BaiduOCROnline::bankCardScanResult,this,&MainWindow::displayResult);
    connect(BaiduOCR,&BaiduOCROnline::vehicleCardScanResult,this,&MainWindow::displayResult);
    connect(BaiduOCR,&BaiduOCROnline::driveCardScanResult,this,&MainWindow::displayResult);
    connect(BaiduOCR,&BaiduOCROnline::licensePlateScanResult,this,&MainWindow::displayResult);
    connect(BaiduOCR,&BaiduOCROnline::vat_invoiceScanResult,this,&MainWindow::displayResult);
    connect(BaiduOCR,&BaiduOCROnline::quota_invoiceScanResult,this,&MainWindow::displayResult);

    connect(ui->cameraview, &CameraView::fcaptrued, this, &MainWindow::readCapturedImage);
    connect(ui->cameraview, &CameraView::icaptrued, this, &MainWindow::reciveCapturedImage);

    QString file_path = QApplication::applicationDirPath() + "/userParameters/config.ini";
    iniFile = new QSettings(file_path, QSettings::IniFormat, this);
    if(!QFile::exists(file_path))
    {
        QMessageBox::information(this, "提示", "未找到文件" + file_path + ".\n请设置应用参数.");
    }
    else
    {
        iniFile->beginGroup("API-parameters");
        BaiduOCR->getAccToken(iniFile->value("API_key").toString(), iniFile->value("Secret_key").toString());
        iniFile->endGroup();
    }
    Image.load(QApplication::applicationDirPath() + "/temp.jpg");
    Image.scaled(ui->image_view->size());
    ui->image_view->setPixmap(QPixmap::fromImage(Image));
}

MainWindow::~MainWindow()
{
    delete ui;
}

//void MainWindow::changeEvent(QEvent *event)
//{
//    if(event->type() == QEvent::WindowStateChange)
//    {
//        if(windowState() == Qt::WindowMinimized)
//        {
//            if(isScreenshot)
//            {
//                swidget->showFullScreen();
//            }
//        }
//    }
//}

void MainWindow::reciveCapturedImage(int id, const QImage& preview)
{
    Q_UNUSED(id);
    ui->stackedWidget->setCurrentIndex(0);
    Image = preview;
    ui->image_view->setPixmap(QPixmap::fromImage(Image));
//    on_act_baidu_triggered();
    QTimer::singleShot(3000, [this]{
        ui->stackedWidget->setCurrentIndex(1);
    });
}
void MainWindow::readCapturedImage(int id, const QString& fileName)
{
    Q_UNUSED(id);
    ui->image_path->setText(fileName);
    on_act_baidu_triggered();
}

void MainWindow::setIniFile(const QString& api_key, const QString& secret_key)
{
    iniFile->clear();
    iniFile->beginGroup("API-parameters");
    iniFile->setValue("API_key", api_key);
    iniFile->setValue("Secret_key", secret_key);
    iniFile->endGroup();

    BaiduOCR->getAccToken(api_key, secret_key);
}

void MainWindow::displayResult(const QString& content)
{
    ui->textBrowser->append(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
    ui->textBrowser->append(content);
}
void MainWindow::on_open_img_triggered()
{
    ui->stackedWidget->setCurrentIndex(0);
    QString filename = QFileDialog::getOpenFileName();
    if(!filename.isEmpty())
    {
        ui->image_path->setText(filename);
        Image.load(filename);
        Image.scaled(ui->image_view->geometry().width(),ui->image_view->geometry().height(),Qt::KeepAspectRatio);
        ui->image_view->setPixmap(QPixmap::fromImage(Image));
    }
}

void MainWindow::on_act_baidu_triggered()
{
    QString Image = ui->image_path->text();
    if(Image.isEmpty())
    {
        QMessageBox::information(0,"提示","文件路径为空");
        return;
    }
    switch (ButtonGroup->checkedId())
    {
        case 0:
        {
            BaiduOCR->StdOCR(Image);
            break;
        }
        case 1:
        {
            BaiduOCR->PreciseOCR(Image);
            break;
        }
        case 2:
        {
            BaiduOCR->idCardScan(Image, id_card_fside);
            break;
        }
        case 3:
        {
            BaiduOCR->bankCardScan(Image);
            break;
        }
        case 4:
        {
            BaiduOCR->vehicleCardScan(Image);
            break;
        }
        case 5:
        {
            BaiduOCR->driveCardScan(Image);
            break;
        }
        case 6:
        {
            BaiduOCR->licensePlateScan(Image);
            break;
        }
        case 7:
        {
            BaiduOCR->vat_invoiceScan(Image);
            break;
        }
        case 8:
        {
            BaiduOCR->quota_invoiceScan(Image);
            break;
        }
    }
}

void MainWindow::drawPixmap(const QPixmap& pixmap)
{
    setWindowState(Qt::WindowNoState);
    isScreenshot = false;
    ui->stackedWidget->setCurrentIndex(0);
    ui->image_path->setText(QString("%1/screen_shot.png").arg(qApp->applicationDirPath()));
    ui->image_view->setPixmap(pixmap.scaled(ui->image_view->size(),Qt::KeepAspectRatio));
}
void MainWindow::on_shot_screen_triggered()
{
    setWindowState(Qt::WindowMinimized);
    QTimer::singleShot(500, swidget, &ScreenWidget::showFullScreen);
}

void MainWindow::on_actHelp_triggered()
{
    QMessageBox::information(this,"快捷键说明","打开图片：Ctrl + O\n面向百度：Ctrl + U\n截图：Alt + S");
}

void MainWindow::on_actSetPara_triggered()
{
    Parameters ParaDialog;
    ParaDialog.exec();
    if(ParaDialog.result() == QDialog::Accepted)
    {
        setIniFile(ParaDialog.API_key(), ParaDialog.Secret_key());
    }
}

void MainWindow::on_actAbout_triggered()
{
    QMessageBox::information(this, "关于", "作者：香菇滑稽之谈\n主页：blog.csdn.net/wwplh5520370\nwechat：784765727");
}

void MainWindow::on_ActOpenCamera_triggered()
{
    ui->stackedWidget->setCurrentIndex(1);
    ui->cameraview->openCamera();
}

void MainWindow::on_ActCaptrue_triggered()
{
    ui->cameraview->captrueImage();
}

void MainWindow::on_ActCloseCamera_triggered()
{
    ui->stackedWidget->setCurrentIndex(0);
    ui->cameraview->closeCamera();
}

